package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.BannerJumpDtoMapper;
import com.starlink.model.domain.BannerJumpDO;
import com.starlink.mapper.BannerJumpMapper;
import com.starlink.model.request.BannerJumpRequest;
import com.starlink.service.BannerJumpService;
import com.starlink.model.util.BannerJumpConvert;
import com.starlink.model.vo.BannerJumpVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class BannerJumpServiceImpl extends ServiceImpl<BannerJumpMapper, BannerJumpDO> implements BannerJumpService {
    @Autowired
    private BannerJumpMapper bannerJumpMapper;

    @Override
    public BannerJumpVO getByKey(long id) {
        BannerJumpVO bannerJumpVO = BannerJumpDtoMapper.INSTANCE.do2VO(getById(id));
        return bannerJumpVO;
    }

    @Override
    public PageInfo<BannerJumpVO> pageQuery(Pageable pageable,BannerJumpRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<BannerJumpDO> list = bannerJumpMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<BannerJumpVO> bannerJumpVOList = BannerJumpConvert.buildBannerJumpVoList(list);
        return new PageInfo(bannerJumpVOList);
    }

    @Override
    public List<BannerJumpVO> listQuery(BannerJumpRequest request) {
        List<BannerJumpDO> list = bannerJumpMapper.selectDOList(BannerJumpDtoMapper.INSTANCE.request2DO(request));
        List<BannerJumpVO> bannerJumpVOS = BannerJumpConvert.buildBannerJumpVoList(list);
        return bannerJumpVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(BannerJumpRequest request) {
        save(BannerJumpDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(BannerJumpRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(BannerJumpDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        bannerJumpMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
